package com.jialidun.jiaowusys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jialidun.jiaowusys.common.CustomException;
import com.jialidun.jiaowusys.common.R;
import com.jialidun.jiaowusys.entity.Book;
import com.jialidun.jiaowusys.service.BookService;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;




    @GetMapping
    public R getAll() {
        List<Book> list = bookService.list();
        return R.success(list);
    }

    @GetMapping("/id")
    public R<Book> getByid(Long id) {

        Book byId = bookService.getById(id);
        return R.success(byId);

    }

    @PutMapping
    public R<String> uptate(@RequestBody Book book) {
        bookService.updateById(book);
        return R.success("修改成功");
    }

    @PostMapping
    public R add(@RequestBody Book book) {
        bookService.save(book);
        return R.success("添加成功");
    }

    @DeleteMapping("/{id}")
    public R shanchuById(@PathVariable int id){

        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<Book>();
        queryWrapper.eq(Book::getId,id);
        bookService.removeById(id);
        return R.success("删除成功");
    }
    @GetMapping("/page")
    public R<Page<Book>> page(Integer str, Integer end, String type){
        Page<Book> page = new Page<>(str,end);
        LambdaQueryWrapper<Book> booklqw = new LambdaQueryWrapper<>();
        booklqw.like(StringUtils.isNotEmpty(type),Book::getType,type);
        Page<Book> page1 = bookService.page(page, booklqw);
        return R.success(page1);
    }

    @PostMapping("/now")
    public R DateformatToday(){

        // 调用 sevice
        Date date = new Date();


        return R.success(date);
    }

}


